﻿using System;
using System.Collections.Generic;
using System.Linq;

using SharedGenomics.Core;
using SharedGenomics.Annotations;

namespace SharedGenomics.Workbench.Annotations
{
    public class RepositoryUpdateAnnotationRequestStage : IAnnotationRequestStage
    {
        private readonly IAnnotationRepositoryMediator _annotations;

        public RepositoryUpdateAnnotationRequestStage(IAnnotationRepositoryMediator annotations)
        {
            this._annotations = annotations;
        }

        public void Process(AnnotationRequestContext context, IAnnotationRequestPipelineTraversalStrategy traversalStrategy)
        {
            context.OutputArtifacts.ForEach(a =>
            {
                this._annotations.Add(a.Key, a.Value, a.ParentValue);
            });
            //if (context.Annotation != null && !context.ExistsInRepository)
            //{
            //    //add annotation to repository since it has not already been added
            //    //NOTE: it is possible that the annotation has been added since ExistsInRepository was set but this doesn't matter since we will just 
            //    //overwrite the previous version
            //    this._annotations.Add(context.RequestKey, context.Annotation, context.ParentValue);

            //    //TODO: add all associated annotation values
            //    context.AnnotationValues.ForEach(v => this._annotations.Add(v.Key, v.Value, new AnnotationValue(AnnotationKey.SnpName, (string)context.AnnotationTarget))); 
            //}

            traversalStrategy.MoveNext(context);
        }
    }
}
